#include <glib.h>
#include <gio/gio.h>

int main(int argc, char *argv[])
{

    GError *error = NULL;

    GSocketClient * client = g_socket_client_new();

    GSocketConnection * connection = g_socket_client_connect_to_host (client,"www.baidu.com",80,NULL,&error);

    if (error){
        g_error(error->message);
    }else{
        g_message("Connection ok");
    }
    g_object_unref(client);

    gchar *buffer="GET / HTTP/2.0\r\n\r\n";
    gchar incoming_buff[100]={0};

    GInputStream * in_stream = g_io_stream_get_input_stream(G_IO_STREAM(connection));
    GOutputStream * out_stream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
    g_output_stream_write(out_stream, buffer, strlen(buffer), NULL, &error);
    int incoming_num = g_input_stream_read(in_stream,incoming_buff,100,NULL,&error);

    if(error)
        g_error(error->message);
    else{
        g_print("incoming: %s\n", incoming_buff);
    }
    g_io_stream_close(G_IO_STREAM(connection), NULL, &error);

    return TRUE;
}
